#### LIBRARIES ####
library(tidyverse)
library(ggstance)
library(foreign)

#### IMPORT AND CLEAN DATA ####
#### CCES 2006 ####
load("Data/cces_2006_common.RData")

cces06 <- x%>%
  mutate(post.pid=(as.numeric(v4034)!=3),
         pre.pid=(as.numeric(v3005)!=3&as.numeric(v3005)!=5),
         voteval=as.numeric(as.numeric(vote_gen06)==1))

cces06$voteval[as.numeric(cces06$vote_gen06)==7|
                 as.numeric(cces06$vote_gen06)==8] <- NA


#### CCES 2008 ####
load("Data/cces_2008_common.RData")

cces08 <- x%>%
  mutate(post.pid=(as.numeric(CC423)!=3&as.numeric(CC423)!=5),
         pre.pid=(as.numeric(CC307)!=3&as.numeric(CC307)!=5),
         voteval=as.numeric(as.numeric(vote_gen08)==1))

cces08$voteval[as.numeric(cces08$vote_gen08)==7|
                 as.numeric(cces08$vote_gen08)==8] <- NA

#### CCES 2010 ####
cces10 <- read.dta("Data/cces_2010_common_validated.dta")

cces10 <- cces10%>%
  mutate(post.pid=(as.numeric(CC421a)!=3),
         pre.pid=(as.numeric(V212a)!=3&as.numeric(V212a)!=5),
         vote=as.numeric(CC401)==5,
         voteval=as.numeric(as.numeric(vote_gen10)==1))
cces10$voteval[as.numeric(cces10$vote_gen10)>6] <- NA


#### CCES 2012 ####
load("Data/CCES12_Common_VV.RData")

cces12 <- x%>%
  mutate(post.pid=(as.numeric(CC421a)!=3),
         pre.pid=(as.numeric(pid3)!=3&as.numeric(pid3)!=5),
         vote=as.numeric(CC401)==5,
         voteval=as.numeric(e2012g!="MatchedNoVote"))


#### CCES 2014 ####
load("Data/CCES14_Common_Content_Validated.RData")

x <- x%>%
  mutate(vote=as.numeric(e2014gvm!=""))
x$vote[is.na(x$vote)] <- 0
x$vote[x$matched==0] <- NA

cces14 <- x%>%
  mutate(pre.pid=(as.numeric(pid3)!=3&as.numeric(pid3)!=5),
         post.pid=(as.numeric(CC421a)!=3),
         voteval=vote)


#### CCES 2016 ####
load("Data/CCES16_Common_OUTPUT_Feb2018_VV.RData")

x <- x%>%
  mutate(vote=as.numeric(CL_E2016GVM!=""))
x$vote[is.na(x$vote)] <- 0
x$vote[x$CL_matched==0] <- NA

cces16 <- x%>%
  mutate(pre.pid=(as.numeric(pid3)!=3&as.numeric(pid3)!=5),
         post.pid=(as.numeric(CC16_421a)!=3),
         voteval=vote)


#### CCES 2018 ####
load("Data/cces18_common_vv.RData")

x$voteval <- 1-is.na(x$CL_2018gvm)
x$voteval[x$CL_matched==1] <- NA

cces18 <- x%>%
  mutate(pre.pid=(as.numeric(pid3)!=3&as.numeric(pid3)!=5),
         post.pid=(as.numeric(CC18_421a)!=3))


#### REGRESSIONS ####

year <- seq(2006,2018,2)
control <- c(1,0)
res2 <- expand.grid(control,year)
names(res2) <- c("control","year")
res2$beta <- NA
res2$sigma <- NA


res2$beta[res2$year==2006&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces06))$coefficients[2,1]
res2$beta[res2$year==2008&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces08))$coefficients[2,1]
res2$beta[res2$year==2010&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces10))$coefficients[2,1]
res2$beta[res2$year==2012&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces12))$coefficients[2,1]
res2$beta[res2$year==2014&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces14))$coefficients[2,1]
res2$beta[res2$year==2016&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces16))$coefficients[2,1]
res2$beta[res2$year==2018&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces18))$coefficients[2,1]

res2$sigma[res2$year==2006&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces06))$coefficients[2,2]
res2$sigma[res2$year==2008&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces08))$coefficients[2,2]
res2$sigma[res2$year==2010&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces10))$coefficients[2,2]
res2$sigma[res2$year==2012&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces12))$coefficients[2,2]
res2$sigma[res2$year==2014&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces14))$coefficients[2,2]
res2$sigma[res2$year==2016&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces16))$coefficients[2,2]
res2$sigma[res2$year==2018&res2$control==1] <- 
  summary(lm(post.pid ~ voteval + pre.pid,data=cces18))$coefficients[2,2]


res2$beta[res2$year==2006&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces06))$coefficients[2,1]
res2$beta[res2$year==2008&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces08))$coefficients[2,1]
res2$beta[res2$year==2010&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces10))$coefficients[2,1]
res2$beta[res2$year==2012&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces12))$coefficients[2,1]
res2$beta[res2$year==2014&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces14))$coefficients[2,1]
res2$beta[res2$year==2016&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces16))$coefficients[2,1]
res2$beta[res2$year==2018&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces18))$coefficients[2,1]

res2$sigma[res2$year==2006&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces06))$coefficients[2,2]
res2$sigma[res2$year==2008&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces08))$coefficients[2,2]
res2$sigma[res2$year==2010&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces10))$coefficients[2,2]
res2$sigma[res2$year==2012&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces12))$coefficients[2,2]
res2$sigma[res2$year==2014&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces14))$coefficients[2,2]
res2$sigma[res2$year==2016&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces16))$coefficients[2,2]
res2$sigma[res2$year==2018&res2$control==0] <- 
  summary(lm(post.pid ~ voteval ,data=cces18))$coefficients[2,2]


#### COEFFICIENT PLOT: FIGURE 9 ####

ggplot(res2,aes(x=beta,y=year,color=as.factor(control)))+
  geom_vline(xintercept = 0,color="grey50")+
  geom_point(position=position_dodgev(height=1/2))+
  geom_errorbarh(aes(xmax=beta+1.96*sigma,xmin=beta-1.96*sigma),height=0,
                 position=position_dodgev(height=1/2))+
  scale_color_manual(values=c("skyblue3","forestgreen"),
                     name="Control for\nPre-Election\nIdentification?",
                     labels=c("No", "Yes"))+
  scale_y_continuous(breaks=seq(2006,2018,2),minor_breaks = c(2005,2019),
                     labels = seq(2006,2018,2))+
  ylab("Year")+
  xlab("Effect of Voting on Post-Election Partisanship")+
  theme_bw()


 ggsave(filename="Figures/Fig9.pdf",width=4.5,height=4.5)
 
 # Average beta estimate, with and without pre-election PID control variable
 res2%>%
   group_by(control)%>%
   summarise(average.effect=mean(beta))

